import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import ElementUI from "element-ui"; // 引入element ui组件
import "element-ui/lib/theme-chalk/index.css";

Vue.use(ElementUI); // 使用element
Vue.config.productionTip = false;


import axios from './plugin.js'
Vue.use(axios);

// 全局时间戳过滤
Vue.filter(
  'formatDate',
  function (timestramp = '', fmt = 'yyyy-MM-dd hh:mm:ss', calcDiffer) {
    if (!timestramp) return ''
    timestramp = +timestramp
    if (!calcDiffer) {
      // yyyy-MM-dd hh:mm:ss
      Date.prototype.Format = function (fmt) {
        let o = {
          'M+': this.getMonth() + 1, //月份
          'd+': this.getDate(), //日
          'h+': this.getHours(), //小时
          'm+': this.getMinutes(), //分
          's+': this.getSeconds(), //秒
          'q+': Math.floor((this.getMonth() + 3) / 3), //季度
          S: this.getMilliseconds() //毫秒
        }
        if (/(y+)/.test(fmt))
          fmt = fmt.replace(
            RegExp.$1,
            (this.getFullYear() + '').substr(4 - RegExp.$1.length)
          )
        for (let k in o)
          if (new RegExp('(' + k + ')').test(fmt))
            fmt = fmt.replace(
              RegExp.$1,
              RegExp.$1.length == 1
                ? o[k]
                : ('00' + o[k]).substr(('' + o[k]).length)
            )
        return fmt
      }
      let now = new Date(timestramp).Format(fmt)
      return now
    }

    // ex：fmt='y~s' 3年2月1天3小时2分1秒
    const UNIT_SEC = 1000,
      UNIT_MIN = 1000 * 60,
      UNIT_HOU = 1000 * 60 * 60,
      UNIT_DAY = 1000 * 60 * 60 * 24,
      UNIT_MON = 1000 * 60 * 60 * 24 * 30,
      UNIT_YEA = 1000 * 60 * 60 * 24 * 365

    const DATE_ENUM = {
      y: '年',
      M: '月',
      d: '天',
      h: '小时',
      m: '分',
      s: '秒'
    }

    let sTime = 0,
      residueTime = timestramp,
      [start, end = 's'] = fmt.split('~')
    let calcTimes = (key) => {
      let calcTime = {
        y: residueTime / UNIT_YEA,
        M: residueTime / UNIT_MON,
        d: residueTime / UNIT_DAY,
        h: residueTime / UNIT_HOU,
        m: residueTime / UNIT_MIN,
        s: residueTime / UNIT_SEC,
        S: residueTime
      }
      return calcTime[key]
    }

    let processKey = (key, tsp) => {
      switch (key) {
        case 'y':
          residueTime -= tsp * UNIT_YEA
          break
        case 'M':
          residueTime -= tsp * UNIT_MON
          break
        case 'd':
          residueTime -= tsp * UNIT_DAY
          break
        case 'h':
          residueTime -= tsp * UNIT_HOU
          break
        case 'm':
          residueTime -= tsp * UNIT_MIN
          break
        case 's':
          residueTime -= tsp * UNIT_SEC
          break
        default:
          residueTime = tsp
          break
      }
    }
    let isStart = false
    for (let k in DATE_ENUM) {
      if (k == start) {
        sTime = parseInt(calcTimes(k))
        isStart = true
        fmt = sTime + DATE_ENUM[k]
        processKey(k, sTime)
      } else if (isStart) {
        sTime = parseInt(calcTimes(k))
        fmt += sTime + DATE_ENUM[k]
        processKey(k, sTime)
        if (k == end) {
          break
        }
      }
    }
    return fmt
  }
)
//全局方法
//时间戳转换日期
Vue.prototype.timestampToDate = function (timestamp){
  timestamp = timestamp ? timestamp : null;
  let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear() + '-';
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
}

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
